package com.zhu.designpatterns.creational.simplefactory;

/**
 * @author ZhuXinYe
 * @date 2022/8/28 16:51
 * @description 简单工厂模式-在类对象的请求者与类之间加一层去处理实例化，并通过分支语句来决定如何实例化
 **/
public class SimpleFactory {
    /**
     * 通过类型获取Car实例对象
     *
     * @param type 类型
     * @return Car
     */
    public static Car createInstance(String type) {
        if ("truck".equals(type)) {
            return new Truck();
        } else if ("suv".equals(type)) {
            return new Suv();
        } else {
            throw new RuntimeException("type[" + type + "]类型不可识别，没有匹配到可实例化的对象！");
        }
    }

    public static void main(String[] args) {
        Car truck = SimpleFactory.createInstance("truck");
        System.out.println("创建的车辆实例为:" + truck.getName());
        Car suv = SimpleFactory.createInstance("suv");
        System.out.println("创建的车辆实例为:" + suv.getName());
    }
}
